function ValueFormatter(data) {
	this._data = data;
	this._index = 0;
}
ValueFormatter.prototype = {
	/**
	 * 计算所有通道，某个采样点的有效值	
	 * index: 采样点下标（第几个采样点）
	 * bOneValue: bOneValue--TRUE=计算一次系统值；FALSE=计算二次系统值
	 * harmonic: Harmonic--谐波次数（基波为1）
	 */
	getValueDataByIndex: function(index, bOneValue, harmonic) {
		harmonic = harmonic || 1;
		var TN = this._getTN(index);
		return this._computeRms(TN, bOneValue, harmonic, index);
	},	
	_getTN: function(index) {
		var TN = 0;
		var sampList = this._data.sampleInfo;
		this._index = index;
		//排序
		for (var i = 0; i < sampList.length; i++) {
			if(!sampList[i]) {
				sampList.splice(i,1);
			}
			var obj = sampList[i];
			for (var j = i; j < sampList.length; j++) {
				if(!sampList[j]) {
					sampList.splice(j,1);
				}
				if(obj.endsamp > sampList[j].endsamp) {
					sampList[i] = smapList[j];
					sampList[j] = obj;
				}
			}
		}
		
		for (var k = 0; k < sampList.length; k++) {
			if (index < sampList[k].endsamp){
				if(sampList[k].endsamp - index < TN) {
					this._index = sampList[k].endsamp - TN;
				}
				TN = sampList[k].samp / 50;
				if (TN <= 0) {
					TN = 1;
				}
				break;
			}
		}
		
		return TN;
	},
	_computeRms : function(TN, bOneValue, harmoic, index){ 
		var sliceNum = Math.ceil(TN + TN / 2);
		var channelArr = this._data.chns;
		var rel = [];
		for (var i = 0; i < channelArr.length; i++) {
			if(!channelArr[i]) {
				channelArr.splice(i,1);
			}
			var channel = channelArr[i];
			if(channel.analyse === 0) {
				continue;
			}
			var dataArr = [];
			for (var j = 0; j < sliceNum; j++) {
				var endIndex = Math.min(channel.y.length - 1, this._index + j);
				dataArr[j] = channel.y[endIndex] * channel.a + channel.b;
			}
			var ftabc = {};
			ftabc.fir = 0;
			ftabc.sec = 0;
			if(TN === 1) {
				if(harmoic == 1) {
					ftabc.fir = dataArr[0];
					ftabc.sec = dataArr[1];
				}
			}else{
				var m = TN >> 1;
				for (var k = 0; k < TN; k++) {
					ftabc.fir += dataArr[k] * Math.sin(k * harmoic * Math.PI / m);
					ftabc.sec += dataArr[k] * Math.cos(k * harmoic * Math.PI / m);
				}
				ftabc.fir /= m;
				ftabc.sec /= m;
			}
			
			var phasor = new MeasPhasor();
			phasor.m_dVra = ftabc.fir;
			phasor.m_dVia = ftabc.sec;
			var bUnit = false;
			var uuStr = channel.uu.toLowerCase();
			if(uuStr.indexOf('k') != -1 && channel.a < 1) {
				bUnit = true;
			}
			var dTempM = phasor.RMSA();
			var dTempA = phasor.AngleA();
			dTempA -= (channel.skew * 2.0 * Math.PI) / 20000;
			var bAlreadOne = channel.ps.toLowerCase().indexOf("p") != -1;
			if (bOneValue){
				if (!bAlreadOne) {
					dTempM *= channel.ptct;
				}
				if (!bUnit)
					dTempM /= 1000.0;//如果单位是KV\KA则不用除1000
			}else{
				if (bAlreadOne) {
					dTempM /= channel.ptct;
				}
			}
			phasor.ToXYA(dTempM, dTempA);
			var reObj = {};
			reObj.name = channel.name;
			reObj.index = i;
			var uuStr = bOneValue ? 'k' : '';
			reObj.valueStr = (Math.sqrt((Math.pow(phasor.m_dVia,2)+Math.pow(phasor.m_dVra,2))/2)).toFixed(3)+uuStr + channel.uu;
			reObj.valueSsz = channel.y[index]*channel.a+channel.b;
			reObj.valueCyz = channel.y[index];
			rel.push(reObj);
			// console.log('pos3='+this._index+"--YSZ="+reObj.valueStr+" SSZ="+reObj.valueSsz+" CYZ="+reObj.valueCyz);
		}
		
		function MeasPhasor() {
			this.m_dVra = 0; //故障前实部
	    	this.m_dVia = 0; //故障前虚部
	    	this.m_dVrp = 0; //故障后实部
	    	this.m_dVip = 0; //故障后虚部
	    	this.m_nZeroValue = 0;
	    	this.RMSP = function() {
	    		return Math.sqrt((this.m_dVrp * this.m_dVrp + this.m_dVip * this.m_dVip) / 2);
	        };
	        this.RMSA = function() {
	            return Math.sqrt((this.m_dVra * this.m_dVra + this.m_dVia * this.m_dVia) / 2);
	        };

	        this.AngleP = function(){
	            if((this.m_dVrp === 0.0) && (this.m_dVip === 0.0)) {
	            	return 0.0;
	            }
	            return Math.atan2(this.m_dVip, this.m_dVrp);
	        };
	        this.AngleA = function(){                           
	            if((this.m_dVra === 0.0) && (this.m_dVia === 0.0)) {
	            	return 0.0;
	            }
	            return Math.atan2(this.m_dVia, this.m_dVra);
	        };                                                             
	        this.ToXYP = function(rms, angle) {
	            this.m_dVrp = (rms * Math.sqrt(2.0)) * Math.cos(angle);
	            this.m_dVip = (rms * Math.sqrt(2.0)) * Math.sin(angle);
	        };
	        this.ToXYA = function(rms, angle) {
	            this.m_dVra = (rms * Math.sqrt(2.0)) * Math.cos(angle);
	            this.m_dVia = (rms * Math.sqrt(2.0)) * Math.sin(angle);
	        };
		}
	    return rel;    
		
	},
	
	
}
